package com.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author mayifan
 * @version 1.0
 * @date 2025-03-04 10:42
 */
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    //在支付宝创建的应用的id
    private String app_id = "9021000145650856";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQChVfhIOw560Ts9PNMG6Y5AzyjsQcFz4ziImkVwPoJD2CZMGlCWQbKEhZGQoD9MvHc3jxwF5oC+l3DMnz4eLTcBn8ly1OIGRB18a8LLSZ8GDLyz1BZYrybFahrI2rnI5ba6hgEHBepq69Bc80FSSCRz1AmPGBef5b5PsdfhsmR1R/DNA76GW8vcxa6V37CpnPz9wwxNtt5u2WjxlZemNQrUBd4zZuABcwIW5qc5Q3w9nCO8fUp9g1GW6tytpKcbisOy5vyRqaw741TCiSedNrpAAvU2JF1eXKLzn/cmslqWNW5GPEIFBsUxuRGdZwOKxaMAVvPyaHKvyZZSaTQQa2HNAgMBAAECggEAV/pjjC/4M21JmplCknBmTF0rebpZ3fI4b/WAjDbqHkkOiU2ISIfG65FS0cUw/41+jWOS6nzKFCTNNChaXWZWqqIR3nbMUpJuadzxup2JBGmJtZ6gGPhacijsNj9gNHdf79MHWGcXb/rsAuP76PzCNA+artd6eo1z/E2E5eS791/H+Fx6T3Ky8rwwKOeME0E3HFg6fLG4DQ+4A9GI+IFO7zT3WXxJKrpUL7NxZFYN4vw63nhF/S5LZu2fApZC3sprQaPZhKo8TNfaN5YS4D/qVCa8nFF8nBJba/eNNinZrHEjX/vJ4j+XYlfGGD8hmEkxrWIQbHOiHgebawQK4CTTAQKBgQDOpeg11oU7KTVY1KuMnAq8OgiQCxheRW2P3E9RJT1kmVOqo2KfdTTvIQoJZK5EsKu32Qw/jgng60e8Bx24WmuhSxhnuuFViFp8HWR4A3xh/xZQTfa+5i5TWA4SlYshM3tV0UzS9qj/0Eoe9GB0yimra+0Syv//YPMxI54hZ+iroQKBgQDH3cCy/lcOgaC7flu/Hf6aAeVUeA4y7BAFlaD0168A9Io9qNUrrmLX9rWvAmrSxUUij3rsuYwldkODVHEIBM4W0KA9wkh7cOamBPFDzkHpgmczqa90DUi340uvQpuZcVHfJI4sGAau1MgvN9oW31zQ847B3Hc7NTWZRPl6JsWmrQKBgQC3Ld4Pi1+Ct96wJaUD+XaSr6bTeOJa/pRGR/aPacFoIXpG8poD27PWnaT1+qd4AFvXlrECzXf4RtfJ100DFDQAQD8cBE6msT30c2GR+RUTETnIabPmOMir3Ixs8N7pyWb1BZliEIByc0Kg+eNe463YZM08MXKIAHrS/k24MxHOwQKBgGy9+R8qBKGu442/hRlpr2GJkIWxwD/ychEnxOgSBXRmmGRN3JWkjsDjyRwMG6TLkf7ow7/SgC7NtpPYuJrdSmfdYKHZd+g5mDRR0hbLPM9YlN/l/cENjTgHoeEcQ15r6liXIgdZlyE+QiB2qeDx7CT/rvdJBVIgdCYNAhN0zusNAoGAZJj9uXbeYuZk4zmTXmUEFjXpsSBcA+FeB6Zo3ZEK8rlWb61P2yj+ggNXQQUgvVIT9to/nkCZX1kXpJ7tAra0D35FqkamHLZNxQW9X/CZOmOSd5tkKeik0nl+TNDneymPBbmMWXcy49mJTYJyH5nL4FkA6pqH/WoX54dIWSO7ICA=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4RV5n9SU1X8jbRVqehggvC77c41gAkCCYUBw3VARjCksR1dUB4V/Le3TRyyCd1oZs9/0EfAR74AfPAJXAGwxWS02H+1ePyvoAdmiiKFO2ky4hNOU5E44ruwBoHXL08J0hEHIaB8ZWPS6UwmR07fWBcikxh7dHvJcR39cELriBPMfuEfowWx8gFeigdqe+yzALl0wmPK0bwoYt51gBONZ81RJin8vl0Tdc1kqvdqMYsQJQuZWOf+nieqy3EbYB8/AnYHHTrOEX2wX51ROSCs7Pkptc2EyGqab7GBWhljG89IckqGtla68z52CmEPFs4Lc80k+CfBVw93mWKvMwd97eQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
// 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://2nzf6x.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
//同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}